#!/bin/bash
echo "=== DevOps Platform Complete Setup and Build ==="

# 1. 创建构建脚本目录和文件
echo "1. Creating build scripts..."
mkdir -p /d/vc/devops-platform/tools/scripts

# 创建 VS2022 构建脚本
cat > /d/vc/devops-platform/tools/scripts/build-vs2022.bat << 'BATEOF'
@echo off
echo === Building with Visual Studio 2022 ===

REM Setup VS2022 environment
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat" -arch=x64

cd /d D:\vc\devops-platform

REM Clean previous build
if exist build rmdir /s /q build
mkdir build
cd build

echo 1. Running CMake configuration...
cmake .. -G "Visual Studio 17 2022" -A x64

echo 2. Building project...
cmake --build . --config Release --parallel

if %errorlevel% equ 0 (
    echo [SUCCESS] Build completed! Executable: build/Release/devops-platform.exe
) else (
    echo [ERROR] Build failed!
    exit /b 1
)
BATEOF

# 创建 MSYS2 构建脚本
cat > /d/vc/devops-platform/tools/scripts/build-msys2.sh << 'SHEOF'
#!/bin/bash
echo "=== Building with MSYS2 MinGW ==="

cd /d/vc/devops-platform

# Clean previous build
if [ -d "build" ]; then
    echo "Cleaning previous build..."
    rm -rf build
fi

mkdir build
cd build

echo "1. Running CMake configuration..."
cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release

echo "2. Building project..."
make -j4

if [ $? -eq 0 ]; then
    echo "[SUCCESS] Build completed! Executable: ./devops-platform.exe"
else
    echo "[ERROR] Build failed!"
    exit 1
fi
SHEOF

chmod +x /d/vc/devops-platform/tools/scripts/build-msys2.sh

# 2. 创建 CMakeLists.txt
echo "2. Creating CMakeLists.txt..."
cat > /d/vc/devops-platform/CMakeLists.txt << 'CMAKEEOF'
cmake_minimum_required(VERSION 3.15)
project(DevOpsPlatform)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Create a simple executable for testing
add_executable(devops-platform
    src/main.cpp
    src/auth/OAuth2Server.cpp
)

# For now, just create a simple main if files don't exist
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
    file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp"
        "#include <iostream>\nint main() { std::cout << \\\"DevOps Platform Running!\\\" << std::endl; return 0; }\n"
    )
endif()

if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/auth/OAuth2Server.cpp")
    file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/src/auth/OAuth2Server.cpp"
        "// OAuth2 Server implementation\n"
    )
endif()
CMAKEEOF

# 3. 创建源码目录和基本文件
echo "3. Creating source code structure..."
mkdir -p /d/vc/devops-platform/src/auth

# 创建简单的主程序用于测试
cat > /d/vc/devops-platform/src/main.cpp << 'MAINEOF'
#include <iostream>
#include "auth/OAuth2Server.h"

int main() {
    std::cout << "=== DevOps Platform ===" << std::endl;
    std::cout << "OAuth2 Server: Initialized" << std::endl;
    std::cout << "Git Integration: Ready" << std::endl;
    std::cout << "CI/CD Engine: Standby" << std::endl;
    std::cout << "=========================" << std::endl;
    return 0;
}
MAINEOF

# 创建头文件
cat > /d/vc/devops-platform/src/auth/OAuth2Server.h << 'HEADEREOF'
#pragma once

class OAuth2Server {
public:
    OAuth2Server() = default;
    void initialize();
};
HEADEREOF

# 创建实现文件
cat > /d/vc/devops-platform/src/auth/OAuth2Server.cpp << 'CPPEOF'
#include "OAuth2Server.h"
#include <iostream>

void OAuth2Server::initialize() {
    std::cout << "OAuth2 Server initialized" << std::endl;
}
CPPEOF

# 4. 测试构建功能
echo "4. Testing build functionality..."
cd /d/vc/devops-platform/tools

echo "=== Toolchain Detection ==="
./build-manager-fixed detect

echo ""
echo "=== Building with VS2022 ==="
./build-manager-fixed vs2022

BUILD_RESULT=$?

# 6. 创建快速开发脚本
echo "6. Creating quick development script..."
cat > /d/vc/devops-platform/quick-dev.sh << 'QUICKEOF'
#!/bin/bash
echo "=== Quick DevOps Development ==="

cd /d/vc/devops-platform/tools

echo "1. Toolchain status:"
./build-manager-fixed detect

echo ""
echo "2. Building project..."
./build-manager-fixed vs2022

if [ $? -eq 0 ]; then
    echo ""
    echo "3. Running application..."
    cd ../build
    if [ -f "Release/devops-platform.exe" ]; then
        ./Release/devops-platform.exe
    elif [ -f "devops-platform.exe" ]; then
        ./devops-platform.exe
    else
        echo "[ERROR] Executable not found!"
    fi
else
    echo "[ERROR] Build failed!"
fi
QUICKEOF

chmod +x /d/vc/devops-platform/quick-dev.sh

echo ""
echo "=== Setup Complete ==="
if [ $BUILD_RESULT -eq 0 ]; then
    echo "✅ All components setup successfully!"
    echo "🚀 You can now run: ./quick-dev.sh"
else
    echo "⚠️  Setup completed with some build issues"
    echo "💡 Check the build output above for details"
fi

echo ""
echo "Quick commands:"
echo "  ./quick-dev.sh           - Build and run"
echo "  ./tools/build-manager-fixed detect  - Check toolchains"
echo "  ./tools/build-manager-fixed vs2022  - Build with VS2022"
echo "  ./tools/build-manager-fixed msys2   - Build with MSYS2"
